# 读取本目录中的cake_page.html中指定的信息，并存入txt文本中
import lxml.html

with open('cake_page.html', 'r', encoding="utf-8") as file:
    my_html = file.read()  # 读取文件中的html代码
    # print(my_html)
selectors = lxml.html.fromstring(my_html)
filter_names = selectors.xpath('//div[@class="s-l-tit filter_attr_name"]/text()')
print(filter_names)  # 所有主分类标题数据
# price_auto fl
price_autos = selectors.xpath('//div[@class="price_auto fl"]')  # 子分类的容器
print(price_autos)  # 集合，Element div
price_auto_names = []
write_text = ''  # 存放待写入内容
for price_aux in price_autos:
    index = price_autos.index(price_aux)
    names = price_aux.xpath('.//div/a/text()')  # 所有子分类的名称
    print(filter_names[index])
    print(names)
    write_text += filter_names[index]+'\n'
    write_text += '、'.join(names)+'\n'

# 向文本文件写入内容
with open('cake_filter_data.txt', 'w', encoding="utf-8") as cake_file:
    cake_file.write(write_text)




# 写入txt文本的格式
# 对象：
# 全部、生日蛋糕、毕业蛋糕
# 类型：
